package com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.R;
import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Utils.Constant;
import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Utils.PayListener;
import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Utils.PaySDK;
import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Utils.PaySDKFactory;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class CardPayActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_pay);
        initData();


    }

    private void initData() {
        Intent intent = getIntent();
        String price = intent.getStringExtra("price");

        //因为兴业银行的交易是以“分” 为单位，我们输入的是以“元为单位” 所以将元转换成分
        float i = Float.valueOf(price);
        final int newmoneyNumber= (int) (i*100);

        JSONObject data = new JSONObject();
        try {
            data.put("appid", "1100011000");//接入渠道id
            data.put("phone", "40WX4I0M01");//手机号
            data.put("amount", newmoneyNumber);//价格
            data.put("mchnt_no", "309044353310001");//商户号
            data.put("trans_type", "01");//交易类型 1 消费 2 撤销 3 退货
        } catch (JSONException e) {
            e.printStackTrace();
        }

        OkGo.<String>post(Constant.CREATE_ORDER_URL)
                .upJson(data.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String s = response.toString();
                        String body = response.body();
                        try {
                            JSONObject object = new JSONObject(body);
                            String orderId = object.optString("order_id");
                            toPayApp(orderId);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        Log.i("TAG", "Success---" + body);
                    }

                    @Override
                    public void onError(Response<String> response) {

                        Log.e("TAG", response.toString());

                        response.getException().printStackTrace();
                    }
                });
    }

    private void toPayApp(String orderid) {
        PaySDK paysdk = PaySDKFactory.getInstance(this);
        paysdk.setPayListener(new PayListener() {

            @Override
            public void onSuccess(Map<String, Object> message) {
                // TODO Auto-generated method stub
                Log.i("TAG", "支付完成...返回成功信息 === ppppp " + message);
                if (message.get("resCode") != null)
                    Toast.makeText(CardPayActivity.this, (String) message.get("resCode"), Toast.LENGTH_LONG).show();

                // MainActivity.this.finish();
//                requestState();
            }

            @Override
            public void onFail(String message) {
                // TODO Auto-generated method stub
                Log.i("TAG", "支付完成...返回失败信息 = " + message);
                Toast.makeText(CardPayActivity.this, message, Toast.LENGTH_LONG).show();

            }

        });
            if (orderid != null) {
                //Map<String, Object> sendData = new HashMap<String, Object>();
                Map<String, Object> map = new HashMap();
                map.put("mchntNo", "309044348120034");//商户号 309044348120034
                map.put("phone", "40WX4I0M01");//手机号
                map.put("opeNum", "01");//操作员号
                map.put("terminalNo", "43008388");//终端编号 43008388
                map.put("mchntName", "hahah"); //商品名
                map.put("transType", "cardPay");//交易类型
                map.put("transactionId", orderid);//订单号
                Log.i("TAG", "往支付app发送的数据data = " + map);
                paysdk.toPayApp(CardPayActivity.this, "cc.rengu.android.pay.ipps",
                        "cc.rengu.android.ui.activity.CollectionMoney", map);
            } else {
                Toast.makeText(this, "获取订单号失败！", Toast.LENGTH_LONG).show();
            }
    }
}
